package com.panghl.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Author panghl
 * @Date 2022/2/13
 * @Description quickstart 测试使用
 **/
@Controller
public class MyController {

    @GetMapping("/")
    public String toIndex(Model model) {
        model.addAttribute("msg", "helloWorld");
        return "index";
    }

    @GetMapping("/user/add")
    public String add() {
        return "user/add";
    }

    @GetMapping("/user/update")
    public String update() {
        return "user/update";
    }

    @GetMapping("/user/u1")
    public String update1() {
        return "user/update";
    }

    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @GetMapping("/login")
    public String login(String username, String password,Model model) {
        //获取当前的用户
        Subject currentUser = SecurityUtils.getSubject();
        //封装用户的登录信息
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        // 判断当前的用户是否被认证
        //if (!currentUser.isAuthenticated()) {
            try {
                currentUser.login(token); //执行登录方法，如果没有异常就OK
                model.addAttribute("msg","登录成功");
                return "index";
            } catch (Exception e) {
                model.addAttribute("msg","登录失败");
                return "login";
            }
    }



}
